package com.itggm.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itggm.Result.ResponseResult;

import com.itggm.dto.AddRolePermissionsDTO;
import com.itggm.dto.RoleDTO;
import com.itggm.dto.RolePageDTO;
import com.itggm.exception.BusinessException;
import com.itggm.pojo.Role;
import com.itggm.service.RoleService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author: ggm
 * @data 2024年12月21日 14:25
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @GetMapping("list")
    public ResponseResult roleList() {
        log.info("查询角色列表");
        List<Role> list = roleService.list();
        return ResponseResult.success().data("roleList", list);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 分页查询
     */
    @PostMapping("/page")
    public ResponseResult rolePage(@RequestBody RolePageDTO rolePageDTO) {
        log.info("角色分页查询{}", rolePageDTO);
        return roleService.rolePage(rolePageDTO);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 添加或编辑角色信息
     */
    @PostMapping("/addOrUpdate")
    public ResponseResult addOrUpdate(@RequestBody Role role) {
        log.info("添加或编辑角色信息{}", role);
        //  role 实体类中id属性不为null则更新对象    否则添加数据
        try {
            roleService.saveOrUpdate(role);
        } catch (DuplicateKeyException e) {
            throw new BusinessException("角色名或权限字符重复");
        }
        return ResponseResult.success();
    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 删除角色信息
     */
    @DeleteMapping("/delete/{rid}")
    public ResponseResult deleteRole(@PathVariable Integer rid) {
        log.info("删除角色{}", rid);
        return roleService.delete(rid);

    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 根据角色id获取角色权限
     */

    @GetMapping("/rolePermissions/{id}")
    public ResponseResult rolePermissions(@PathVariable Integer id) {
        log.info("根据角色id获取角色权限{}",id);
        return roleService.rolePermissions(id);
    }

     /**
      * @Author: ggm
      * @date:  2024/12/23
      * 添加角色权限信息
      */
     @PostMapping("/addRolePermissions")
     public ResponseResult addRolePermissions(@RequestBody AddRolePermissionsDTO addRolePermissionsDTO){
         log.info("添加权限{}",addRolePermissionsDTO);
         return  roleService.addRolePermissions(addRolePermissionsDTO);
     }

}
